/**
 * 
 */
package com.example.ad_project;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import org.json.JSONException;

import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.os.StrictMode.ThreadPolicy;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.Toast;

import com.nus.iss.ad.dbhelper.RetrivalListDBHelper;
import com.nus.iss.ad.model.RetrivalListModel;
import com.nus.iss.ad.utils.MyUtilities;

/**
 * @author Sachin
 * 
 */
public class RetrivalListActivity extends BaseActivity implements
		OnItemClickListener {

	boolean newFlag = false; // if flag false then load from json

	static List<RetrivalListModel> rlist = null;
	RetrivalListDBHelper data = new RetrivalListDBHelper();

	DatePicker datePicker;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		StrictMode.setThreadPolicy(ThreadPolicy.LAX);

		setContentView(R.layout.retrival_list_activity);

		/*
		 * List<User> userList = new ArrayList<User>();
		 * 
		 * userList.add(new User("Sachin", "10")); userList.add(new
		 * User("Sachin", "10")); userList.add(new User("Sachin", "10"));
		 */

		loadData();
		newFlag = true;

		ListView lv = (ListView) findViewById(R.id.listView1);

		RetrivalListModel[] array = rlist.toArray(new RetrivalListModel[rlist
				.size()]);

		datePicker = (DatePicker) findViewById(R.id.datePicker1);

		// Log.d("Test", (System.currentTimeMillis() - 1000)+"");
		// Log.d("Test", (System.currentTimeMillis()+(30*60*1000))+"");

		// /Calendar c = Calendar.getInstance();

		// Log.d("Text", c.getTime()+"");

		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH);
		int day = c.get(Calendar.DAY_OF_MONTH);

		datePicker.updateDate(year, month, day);

		Log.d("Text", (c.getTimeInMillis() - 10000) + "");

		datePicker.setMinDate(c.getTimeInMillis() - 10000);
		// System.currentTimeMillis()+(30*60*1000);
		// datePicker.setMaxDate(System.currentTimeMillis()+(30*60*1000));

		lv.setAdapter(new RetrivalListAdapter(this, array));
		/*
		 * lv.setAdapter(new SimpleAdapter(this, rlist,
		 * R.layout.retrival_list_row_layout, new String[] { "itemName",
		 * "required" , "retrivable"}, new int[] { R.id.textView1, R.id.required
		 * , R.id.retrivable}));
		 */

		lv.setOnItemClickListener(this);

	}

	public void loadData() {

		if (newFlag == true) {

			if (RetrivalDetailsActivity.valueSet.get("selectedId") != null) {

				String id = RetrivalDetailsActivity.valueSet.get("selectedId");
				int value = Integer.parseInt(RetrivalDetailsActivity.valueSet
						.get("value"));

				for (RetrivalListModel r : rlist) {

					if (r.getInventryId().equals(id)) {
						r.setRequired(value);
					}

				}

			}

		} else {

			rlist = new ArrayList<RetrivalListModel>();
			try {
				rlist = data.getRetrivalList();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

	@Override
	public void onResume() {
		super.onResume();

		loadData();

		ListView lv = (ListView) findViewById(R.id.listView1);

		RetrivalListModel[] array = rlist.toArray(new RetrivalListModel[rlist
				.size()]);

		lv.setAdapter(new RetrivalListAdapter(this, array));
		lv.setOnItemClickListener(this);

	}

	@Override
	public void onItemClick(AdapterView<?> av, View v, int position, long id) {

		// Toast.makeText(getApplicationContext(),
		// rlist.get(position).getInventryId() + "Save top selected",
		// Toast.LENGTH_LONG).show();

		Intent i = new Intent(this, RetrivalDetailsActivity.class);
		i.putExtra("selectedId", rlist.get(position).getInventryId());
		startActivity(i);
	}

	public void onSaveAndNotify(View view) {

		boolean checkFlag = false;
		for(int i =0 ; i<rlist.size() ; i++){
			
			if(rlist.get(i).isCheckFlag()){
				checkFlag = true;
			}
			
		}
		
		if(!checkFlag){
		
		int day = datePicker.getDayOfMonth();
		int month = datePicker.getMonth();
		int year = datePicker.getYear();

		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month, day);

		Log.d("text", calendar.getTime() + "");

		try {
			data.addDate(calendar);
			data.checkData();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		data.saveRetrivalList();
		super.onBackPressed();
		
		}
		else {
			Toast.makeText(getApplicationContext(),
					 "Please Check the values",
					 Toast.LENGTH_LONG).show();
			
		}
	}
	
	
	public void onBackPressed() {
		Toast.makeText(getApplicationContext(),
				 "Use Save Button",
				 Toast.LENGTH_LONG).show();
	}

}
